package com.cb.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.eduservice.client.OrdersClient;
import com.cb.eduservice.entity.EduCourse;
import com.cb.eduservice.entity.chapter.ChapterVo;
import com.cb.eduservice.entity.vo.front.CourseFrontVo;
import com.cb.eduservice.entity.vo.front.CourseWebVo;
import com.cb.eduservice.service.EduChapterService;
import com.cb.eduservice.service.EduCourseService;
import com.cb.utils.JwtUtils;
import com.cb.utils.R;
import com.cb.utils.ordervo.CourseWebOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 前台课程管理
 *
 * @author cb
 * @version 1.0
 * @date 2020/6/28 14:40
 */
@Api(description = "前台课程管理")

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    private final EduCourseService courseService;
    private final EduChapterService chapterService;
    private final OrdersClient ordersClient;

    @Autowired
    public CourseFrontController(EduCourseService courseService, EduChapterService chapterService, OrdersClient ordersClient) {
        this.courseService = courseService;
        this.chapterService = chapterService;
        this.ordersClient = ordersClient;
    }

    @ApiOperation("条件分页查询课程")
    @PostMapping("getFrontCourseList/{current}/{limit}")
    public R getFrontCourseList(@PathVariable("current") long current,
                                @PathVariable("limit") long limit,
                                @ApiParam(name="courseFrontVo",value = "查询信息封装",required = false)
                                @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> page = new Page<>(current, limit);
        Map<String,Object> map = courseService.getCourseFrontList(page,courseFrontVo);

        return R.ok().data(map);
    }

    @ApiOperation("根据课程id,查询课程详细信息和对应讲师信息")
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable("courseId") String courseId,
                                HttpServletRequest request){
        // 查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        // 查询章节信息
        List<ChapterVo> chapterAndVideoList = chapterService.getChapterAndVideoByCourseId(courseId);

        // 查询当前用户是否已经支付
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isBuyCourse = false;
        if (!StringUtils.isEmpty(memberId)) {
            isBuyCourse = ordersClient.isBuyCourse(courseId, memberId);
        }

        return R.ok().data("courseInfo", courseWebVo)
                .data("chapterInfo", chapterAndVideoList)
                .data("isBuy", isBuyCourse);
    }

    @ApiOperation("根据课程id查询课程信息")
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseWebOrderVo getCourseInfoOrder(@PathVariable("courseId") String courseId){
        CourseWebVo info = courseService.getBaseCourseInfo(courseId);
        CourseWebOrderVo orderVo = new CourseWebOrderVo();
        BeanUtils.copyProperties(info, orderVo);

        return orderVo;
    }

}
